package com.example.carrierapp.ui.person.modifypass;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.carrierapp.base.BaseActivity;
import com.example.carrierapp.base.BasePresenter;
import com.example.carrierapp.base.MVPBaseActivity;
import com.example.carrierapp.bean.person.ImageCodeBean;
import com.example.carrierapp.config.ConstValue;
import com.example.carrierapp.net.AppClient;
import com.example.carrierapp.ui.view.CreatCode;
import com.example.carrierapp.util.ActivityUtil;
import com.example.carrierapp.util.GlideApp;
import com.example.carrierapp.util.ToastUtils;
import com.example.lb.fingerdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ModifyPassActivity extends MVPBaseActivity<ModifyPassContract.View, ModifyPassPresenter> implements ModifyPassContract.View, CreatCode.OnTextCreatListener {
    @BindView(R.id.cons_step2)
    ConstraintLayout mConsStep2;

    @BindView(R.id.cons_step1)
    ConstraintLayout mConsStep1;
    @BindView(R.id.tv_old_phone)
    TextView mTvOldPhone;
    @BindView(R.id.et_local_code)
    EditText mEtLocalCode;
    @BindView(R.id.ib_local_code)
    ImageView mIbLocalCode;
    @BindView(R.id.et_remote_code)
    EditText mEtRemoteCode;
    @BindView(R.id.et_new_pass)
    EditText mEtNewPass;
    @BindView(R.id.et_new_pass_repeat)
    EditText mEtNewPassAgain;
    @BindView(R.id.tv_login_code)
    TextView mTvCode;
    private String mPhone;
    private ImageCodeBean mImageCodeBean;

    @Override
    public int getContentView() {
        return R.layout.activity_modify_pass;
    }

    @Override
    public String getTitleString() {
        return "修改密码";
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        mIbBack.setOnClickListener(v -> {
            onBack();
        });
        mPhone = getIntent().getStringExtra(ConstValue.VALUE);
        mPresenter.attachView(this);
        mPresenter.getImgaeCode();
        mTvOldPhone.setText("当前号码：" + mPhone);
    }

    private void onBack() {
        if (mConsStep1.getVisibility() == View.VISIBLE)//第一步
        {
            finish();
        } else {
            mConsStep1.setVisibility(View.VISIBLE);
            mConsStep2.setVisibility(View.GONE);
            setTitleString("修改密码");
        }
    }

    @Override
    public void initView() {
        super.initView();
    }

    @OnClick({R.id.tv_next_step, R.id.tv_suer_modify, R.id.ib_local_code, R.id.tv_login_code})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_next_step:

                if (TextUtils.isEmpty(mEtLocalCode.getText().toString().trim())) {
                    ToastUtils.showCenterToast(this, "请输入图形验证码");
                    return;
                }

                if (TextUtils.isEmpty(mEtRemoteCode.getText().toString().trim())) {
                    ToastUtils.showCenterToast(this, "请输入短信验证码");
                    return;
                }
                showProgress();
                mPresenter.nextStep(mPhone,mEtLocalCode.getText().toString().trim(),mImageCodeBean.guid,mEtRemoteCode.getText().toString().trim());

                break;
            case R.id.tv_suer_modify:
                if (TextUtils.isEmpty(mEtNewPass.getText().toString().trim())) {
                    ToastUtils.showCenterToast(this, "请输入新密码");
                    return;
                }

                if (TextUtils.isEmpty(mEtNewPassAgain.getText().toString().trim())) {
                    ToastUtils.showCenterToast(this, "请再输入一次新密码");
                    return;
                }

                if (!mEtNewPass.getText().toString().trim().equals(mEtNewPassAgain.getText().toString().trim())) {
                    ToastUtils.showCenterToast(this, "两次密码输入不一致");
                    return;
                }
                showProgress();
                mPresenter.changePass(mPhone, mEtNewPass.getText().toString().trim(), mEtNewPassAgain.getText().toString().trim(), mEtRemoteCode.getText().toString().trim());

                break;
            case R.id.tv_login_code:
                if (!TextUtils.isEmpty(mEtLocalCode.getText().toString().trim())) {
                    if (mImageCodeBean != null) {
                        showProgress();
                        mPresenter.getMessageCode(mPhone, mEtLocalCode.getText().toString().trim(), mImageCodeBean.guid);

                    } else {
                        ToastUtils.showCenterToast(this, "请重新获取图形验证码");
                    }
                } else {
                    ToastUtils.showCenterToast(this, "请输入图形验证码");
                }
                break;
            case R.id.ib_local_code:
                showProgress();
                mPresenter.getImgaeCode();
                break;
        }
    }

    private CountDownTimer mVerificationTimer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTvCode.setEnabled(false);
            mTvCode.setTextColor(Color.parseColor("#999999"));
            mTvCode.setText(getString(R.string.account_get_verification_code_cd, millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            mTvCode.setEnabled(true);
            mTvCode.setTextColor(Color.parseColor("#2ecc71"));
            mTvCode.setText(getString(R.string.tv_resend_code));
        }
    };

    @Override
    public void onTextCreate(String msg) {
        ToastUtils.showCenterToast(msg, this);
    }

    @Override
    protected boolean onOverrideBackClick() {
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            onBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVerificationTimer.cancel();
    }

    @Override
    protected ModifyPassPresenter createPresenter() {
        return new ModifyPassPresenter(this);
    }

    @Override
    public void getImgaeCodeSuccess(ImageCodeBean bean) {
        dismissProgress();
        if (bean != null) {
            mImageCodeBean = bean;
            mImageCodeBean.imageUrl = "http://10.130.0.128:10020" + bean.imageUrl;
            GlideApp.with(this).load(bean.imageUrl).into(mIbLocalCode);
        }
    }

    @Override
    public void getImgaeCodeField(String msg) {
        dismissProgress();
        ToastUtils.showCenterToast(this, msg);
    }

    @Override
    public void getMessageCodeSuccess() {
        dismissProgress();
        mVerificationTimer.start();
    }

    @Override
    public void getMessageCodeField(String msg) {
        dismissProgress();
        ToastUtils.showCenterToast(this, msg);
    }

    @Override
    public void changePassSuccess() {
        dismissProgress();
        finish();
        ActivityUtil.startLogin(this, true);
    }

    @Override
    public void changePassField(String msg) {
        dismissProgress();
        ToastUtils.showCenterToast(this, msg);
    }

    @Override
    public void nextStepSuccess() {
        dismissProgress();
        mConsStep2.setVisibility(View.VISIBLE);
        mConsStep1.setVisibility(View.GONE);
        setTitleString("验证手机号码");
    }

    @Override
    public void nextStepField(String msg) {
        dismissProgress();
        ToastUtils.showCenterToast(this, msg);
    }
}
